<?php
/**
 * 获取事业部所有的部门，按城市归类
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Hr_OutInterface");
apf_require_class("Bll_Report_Report");
apf_require_class("Report_Urls");
class Ajax_GetFunctionBuController extends AbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        $type = empty($params['type']) ? 0 : intval($params['type']);
        if(!$type){
            $this->json_encode(0,Const_Tip::ERROR_PARAM);
        }
        $data = array();
        $this->report_bll = new Bll_Report_Report();
        $this->city_list = Bll_Hr_Property::get_sort_city_list();
        if($params['type']==1){//入职
            $_where['function_id'] = intval($params['fid']);
            $_where['bu_id'] = intval($params['bu_id']);
            $tmp_data = $this->report_bll->search_on_job_list($_where);
            if(!empty($tmp_data)){
                unset($tmp_data[999]);
                $datalist = array();
                foreach($tmp_data as $city_id=> $val){
                    $datalist[$city_id] = $val;
                    $datalist[$city_id]['city_name'] = $this->city_list[$city_id];
                }
                foreach ($this->city_list as $city_id=>$val){
                    if(!empty($datalist[$city_id])){
                        $data[$city_id] = $datalist[$city_id];
                    }
                }
            }
        }elseif($params['type']==2){//离职
            $data = $this->_get_dimission_data($params);
        }
        $this->json_encode(1,$data);
    }
    private function _get_dimission_data($params){
        $data = array();
        $_where['function_id'] = intval($params['fid']);
        $_where['bu_id'] = intval($params['bu_id']);
        $_where['start'] = intval($params['start']);
        $_where['end'] = intval($params['end']);
        $tmp_data = $this->report_bll->search_dimission_list($_where);

        $allow_depids = $this->report_bll->get_role_dep_ids($this->user_info['user_id']);
        if(!empty($tmp_data)){
            unset($tmp_data[999]);
            $datalist = array();
            foreach($tmp_data as $city_id=> $val){
                $datalist[$city_id] = $val;
                $rate = floor(($val['dimission_num']/($val['on_job_num']+$val['enter_num']))*10000)/10000*100;
                $datalist[$city_id]['rate'] = $rate."%";
                $datalist[$city_id]['city_name'] = $this->city_list[$city_id];
                $url = '';
                if($val['dimission_num'] && in_array($val['department_id'],$allow_depids)){
                    $url = Report_Urls::dimission_view_detail($val['department_id'],$_where['start'],$_where['end']);
                }
                $datalist[$city_id]['url'] = $url;
            }
            $hroa_bll = new Bll_Hr_OutInterface();
            $data_tpl = $this->report_bll->build_data_tpl();
            $filldata = $hroa_bll->get_city_list_by_dep($_where['function_id'],$_where['bu_id']);

            foreach ($this->city_list as $city_id=>$val){
                if(empty($filldata[$city_id])){
                    continue;
                }
                if(empty($datalist[$city_id])){
                    $data[$city_id] = $data_tpl;
                    $data[$city_id]['rate'] = '0%';
                    $data[$city_id]['city_name'] = $val;
                    $data[$city_id]['url'] = '';
                }else{
                    $data[$city_id] = $datalist[$city_id];
                }
            }
        }
        return $data;
    }
}